package cn.edu.hnu.gpg.service;

import cn.edu.hnu.gpg.dao.QuestionMapper;
import cn.edu.hnu.gpg.entity.Question;
import cn.edu.hnu.gpg.entity.QuestionExample;
import cn.edu.hnu.gpg.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class QuestionServiceImpl implements QuestionService {

    private QuestionMapper questionMapper;
    @Autowired
    private StudentService studentService;
    @Autowired
    private UserService userService;

    @Autowired
    public QuestionServiceImpl(QuestionMapper questionMapper){
        this.questionMapper = questionMapper;
    }

//    @Override
//    public List<Question> findQuestionByDescription(String desc) {
//        if(desc.isEmpty())
//            throw new IllegalArgumentException("Query description is empty");
//
//        QuestionExample questionExample = new QuestionExample();
//        return null;
//    }

    @Override
    public List<Question> findQuestionBySubject(String subject) {
        if(subject.isEmpty())
            return null;

        QuestionExample questionExample = new QuestionExample();
        questionExample.or().andSubjectLike(subject);
        return  questionMapper.selectByExampleWithBLOBs(questionExample);
    }

    @Override
    public List<Question> findQuestionByUserId(int user_id) {
        if (user_id <= 0)
            return null;
        Student student = studentService.findStudentByUserId(user_id);
        if (student == null)
            return null;
        QuestionExample questionExample = new QuestionExample();
        questionExample.or().andStudentIdEqualTo(student.getStudentId());
        List<Question> questions = questionMapper.selectByExampleWithBLOBs(questionExample);
        return questions;
    }

    @Override
    public List<Question> findQuestionByStudentId(int student_id) {
        if (student_id<1)
            return null;
        QuestionExample questionExample = new QuestionExample();
        questionExample.or().andStudentIdEqualTo(student_id);
        return questionMapper.selectByExampleWithBLOBs(questionExample);
    }

    @Override
    public int CreateQuestion(String subject, String desc, int user_id) {
        if (subject.isEmpty())
            throw new IllegalArgumentException("subject is empty,please write It!");

        if (desc.isEmpty())
            throw new IllegalArgumentException("description is empty");

        Question question = new Question();
        Student student = studentService.findStudentByUserId(user_id);

        if (student == null)
            throw new IllegalArgumentException("这不是一个有效的学生用户");

        int student_id = student.getStudentId();
        question.setStudentId(student_id);
        question.setDescription(desc);
        question.setSubject(subject);
        //question.setAskTime(new java.sql.Date(new java.util.Date().getTime()));
        return questionMapper.insertSelective(question);
    }
}
